package com.xbongbong.saas.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.formdata.pojo.vo.FormDataVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/03/16 16:16
 */
public class SpecificationUtil {

    public static void getProductName(Integer businessType, String linkKey, JSONObject data, JSONObject result) {
        if (Objects.equals(XbbRefTypeEnum.PRODUCT.getCode(), businessType)
                && Objects.equals(ProductEnum.NAME.getAttr(), linkKey) && Objects.equals(1, data.get(ProductEnum.SPECIFICATION.getAttr()))) {
            String specification = joinSpecification(data.getString(ProductEnum.SPECIFICATION_LINK_TEXT.getAttr()));
            String name = data.getString(linkKey) +"（"+ specification+"）";
            result.put(linkKey, name);
        }
    }

    /**
     * 拼接规格
     * 多规格才做转换
     *
     * @param specification {"规格1":"32G","规格2":"全网通"}
     * @return 32G/全网通
     * @author 周智
     * 创建时间 2017/11/28 16:02
     */
    public static String joinSpecification(String specification) {
        JSONObject specificationObj;
        try {
            specificationObj = JSON.parseObject(specification);
        } catch (Exception e) {
            // 单规格不作处理
            return specification;
        }

        if (specificationObj == null || specificationObj.isEmpty()) {
            return "";
        }

        StringBuilder specificationBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : specificationObj.entrySet()) {
            if (specificationBuilder.length() == 0) {
                specificationBuilder.append(entry.getValue());
            } else {
                specificationBuilder.append("/").append(entry.getValue());
            }
        }

        return specificationBuilder.toString();
    }

    public static void formatChildSpecification4LinkData(List<FormDataVO> paasFormDataESList, String corpid) {
        String specificationAttr = ProductEnum.SPECIFICATION.getAttr();
        String specificationNameAttr = ProductEnum.SPECIFICATION_LINK_TEXT.getAttr();
        String specification;
        for (FormDataVO entityExt : paasFormDataESList){
            JSONObject data = entityExt.getData();
            specification = data.getString(specificationAttr);
            if (specification == null) {
                continue;
            }
            StringBuilder stringBuilder = new StringBuilder();
            if (Objects.equals(specification,"1")){
                JSONObject specifition;
                try {
                    specifition = FastJsonHelper.getJsonObjectOrDefaultFromFormData(data, specificationNameAttr, new JSONObject());
                } catch (XbbException e) {
                    specifition = new JSONObject();
                }
                if (!Objects.isNull(specifition)){
                    for (Map.Entry<String, Object> entry : specifition.entrySet()){
                        StringUtil.stringBuilderAppendSuffix(stringBuilder,"/",entry.getValue());
                    }
                }
            } else {
                specification = data.getString(specificationNameAttr);
                stringBuilder.append(specification);
            }
            data.put(specificationAttr, stringBuilder.toString());
        }
    }
}
